<?php
class controller_product extends model_product{
    private $base_path = array(
        'category' => 'product/category',
    );
    /*
    * view 商品分类列表
    */
    private function category_get_args($get){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$para,'N');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    private function category_page_args($args){
        $page = array();
        if(!empty($args['sKey'])) $page['sKey'] = $args['sKey'];
        if(!empty($args['categoryid'])) $page['categoryid'] = $args['categoryid'];
        if(!empty($args['ps'])) $page['ps'] = $args['ps'];
        $page['page'] = '{page}';
        return $page;
    }
    public function category(){
        $parent_category = $this->mod_parentcategory();
        $args = $this->category_get_args(true);
        $page_args = $this->category_page_args($args);
        $args['page_current'] = $this->get_page();
        $datas = $this->mod_category_list($args);
        $this->display('product/category',array(
            'parent_category' => $parent_category,
            'datas' => $datas,
            'basepath' => 'product/category',
            'page_url' => http_build_query($page_args),
            'searchdata' => $args,
        ));
    }
    /*
    * view 商品分类搜索
    */
    public function category_search(){
        $para = $_GET;

    }
    /*
    * view 添加商品分类
    */
    public function category_add(){
        $parentcategory = $this->mod_parentcategory();
        $this->display('product/category_add',array(
            'basepath' => '/product/category',
            'parentcategory' => $parentcategory,
            'maxsort' => $this->table_maxsort('@@product_category','sort','1'),
        ));
    }
    /*
    * view 编辑商品分类
    */
    public function category_edit(){
        $id = intval($_GET['id']);
        $parentcategory = $this->mod_parentcategory();
        $data = $this->mod_category_item($id);
        $this->display('product/category_edit',array(
            'basepath' => '/product/category',
            'data' => $data,
            'parentcategory' => $parentcategory,
        ));
    }
    /*
    * 商品分类参数处理
    */
    private function category_add_edit_args(){
        $para = $_POST;
        $args['id'] = $this->existsKeyInArray('id',$para,'N');
        $args['title'] = $this->existsKeyInArray('title',$para,'S');
        $args['subtitle'] = $this->existsKeyInArray('subtitle',$para,'S');
        $args['parentid'] = $this->existsKeyInArray('parentid',$para,'S');
        $args['icon'] = $this->existsKeyInArray('icon',$para,'S','#');
        $args['bgcolor'] = $this->existsKeyInArray('bgcolor',$para,'S');
        $args['txtcolor'] = $this->existsKeyInArray('txtcolor',$para,'S');
        $args['parents'] = $this->existsKeyInArray('parentis',$para,'N');
        $args['sort'] = $this->existsKeyInArray('sort',$para,'N');
        if(empty($args['title'])) core::error("分类名称不能为空");
        return $args;
    }
    /*
    * 添加商品分类-保存
    */
    public function category_add_save(){
        if(!$this->post) core::__403();
        $para = $this->category_add_edit_args();
        unset($para['id']);
        $insertid = $this->mod_category_save($para);
        if(!$insertid) core::error("添加[".$para['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$para['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '继续',
            ),
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/category");',
            ),
        ));
    }
    /*
    * 编辑商品分类 - 保存
    */
    public function category_edit_save(){
        if(!$this->post) core::__403();
        $para = $this->category_add_edit_args(true);
        $update_args = array('id'=>intval($para['id']));
        unset($para['id']);
        $updateid = $this->mod_category_save($para,$update_args,true);
        if(!$updateid) core::error("更新[".$para['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$update_args['id'],$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$para['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/category");',
            ),
        ));
    }
    /*
    * 商品分类状态变更 通过 GET 方式传送参数
    * @param $id int 商品分类的ID
    */
    public function category_state(){
        $id = intval($_GET['id']);
        $updateid = $this->update_state('@@product_category',$id);
        if(!$updateid) core::error("状态更新失败");
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "状态更新成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/category");',
            ),
        ));
    }
    /*
     * 商品品牌列表-参数采集过滤
     * @param bool $get 提交方式$_GET或$_POST
     */
    private  function  brands_get_args($get=true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$para,'N');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    private function brands_page_args($args){
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['categoryid'])) $page_args['categoryid'] = $args['categoryid'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    /*
     * 商品品牌列表
     */
    public function brands(){
        $parentcategory = $this->mod_parentcategory();
        $get = true;
        $args = $this->brands_get_args($get);
        $page_args = $this->brands_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_product_brands($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);

        $this->display("product/brands",array(
            'basepath' => '/product/brands',
            'parent_category' => $parentcategory,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'thishref' => '/product/brands',
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    /*
	* view 商品品牌参数处理
	*/
    private function brands_add_edit_args(){
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$_POST,'N');
        $args['title'] = $this->existsKeyInArray('title',$_POST,'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$_POST,'N');
        $args['url'] = $this->existsKeyInArray('url',$_POST,'S');
        $args['icon'] = $this->existsKeyInArray('icon',$_POST,'S');
        $args['info'] = $this->existsKeyInArray('info',$_POST,'S');
        $args['sort'] = $this->existsKeyInArray('sort',$_POST,'N');
        $args['state'] = $this->existsKeyInArray('state',$_POST,'N');
        if(empty($args['title'])) core::error("品牌名称不能为空");
        if(empty($args['icon'])) core::error("品牌LOGO不能为空");
        return $args;
    }
    /*
     * 商品品牌-添加
     */
    public  function  brands_add(){
        $parentcategory = $this->mod_parentcategory();
        $this->display('product/brands_add',array(
            'basepath' => '/product/brands',
            'parentcategory' => $parentcategory,
            'maxsort' => $this->table_maxsort('@@brands','sort','1'),
        ));
    }
    /*
     * 商品品牌-添加-保存
     */
    public  function brands_add_save(){
        if(!$this->post) core::__403();
        $para = $this->brands_add_edit_args();
        $insertid = $this->table_row_save('@@brands',$para);
        if(!$insertid) core::error("添加[".$para['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$para['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '继续',
            ),
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/brands");',
            ),
        ));
    }
    /*
     * 商品品牌-编辑
     */
    public  function  brands_edit(){
        $id = intval($_GET['id']);
        $parentcategory = $this->mod_parentcategory();
        $data = $this->mod_brands_item($id);
        $this->display('product/brands_edit',array(
            'basepath' => '/product/brands',
            'data' => $data,
            'parentcategory' => $parentcategory,
        ));
    }
    /*
        * 编辑商品品牌 - 保存
        */
    public function brands_edit_save(){
        if(!$this->post) core::__403();
        $para = $this->brands_add_edit_args(true);
        $update_args = array('id'=>intval($para['id']));
        unset($para['id']);
        $updateid = $this->table_row_save('@@brands',$para,$update_args,true);
        if(!$updateid) core::error("更新[".$para['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$update_args['id'],$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$para['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/brands");',
            ),
        ));
    }
    /*
     * 删除指定ID的商品品牌
     */
    public function brands_delete(){
        $id = intval($_GET['id']);
        $delid = $this->mod_brands_delete($id);
        if(!$delid) core::error('删除失败');
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/brands");',
            ),
        ));
    }
    /*
     * 删除选定的商品品牌
     */
    public function brands_delete_list(){
        if(!$this->post) core::__403();
        $ids = $this->existsKeyInArray('ids',$_POST,'S');
        if(empty($ids)) core::error('没有选择要删除的数据行');
        $delid = $this->mod_brands_delete_list($ids);
        if(!$delid) core::error('删除失败');
        $this->addLog($this->authid,__FUNCTION__,$delid,$ids);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/brands");',
            ),
        ));
    }
    /*
     * 状态更新
     */
    public function brands_state()
    {
        $id = $this->existsKeyInArray('id',$_REQUEST,'N');
        if(!$id) core::error('参数错误');
        $updateid = $this->update_state('@@brands',$id);
        if(!$updateid) core::error('状态更新失败');
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "状态更新成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/brands");',
            ),
        ));
    }
    /*
    * 商品分类属性
    */
    private function attribute_get_args($get){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$para,'N');
        $args['categorysubid'] = $this->existsKeyInArray('categorysubid',$para,'N');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    private function attribute_page_args($args){
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['categoryid'])) $page_args['categoryid'] = $args['categoryid'];
        if(!empty($args['categorysubid'])) $page_args['categorysubid'] = $args['categorysubid'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    //属性列表
    public function attribute(){
        $get = true;
        $args = $this->attribute_get_args($get);
        $page_args = $this->attribute_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_product_attribute($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);
        $categorysub = array();
        if(!empty($args['categorysubid'])){
            $sub = $this->mod_sub_category($args['categoryid'],'id,title');
            foreach($sub as $v){
                $categorysub[$v['id']] = $v['title'];
            }
        }
        $this->display("product/attribute",array(
            'basepath' => '/product/attribute',
            'parent_category' => $this->mod_parentcategory(),
            'categorysub' =>$categorysub,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    public function attribute_search_select(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        echo json_encode($this->mod_sub_category($id,'id,title'));
    }
    public function attribute_add(){
        $parent_category =$this->mod_parentcategory();
        $parent_class = array();
        foreach($parent_category as $v){
            $parent_class[$v['id']] = $v['title'];
        }
        $this->display('product/attribute_add',array(
            'basepath' => '/product/attribute',
            'parent_category' => $parent_class,
            'datas' => array(),
            'searchdata' => array(),
            'maxsort' => $this->table_maxsort('@@product_attribute','sort','1'),
        ));
    }
    public function attribute_edit(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        $csid = $this->existsKeyInArray('id',$_GET,'N');
        $parent_category =$this->mod_parentcategory();
        $parent_class = array();
        foreach($parent_category as $v){
            $parent_class[$v['id']] = $v['title'];
        }
        $sub = $this->mod_sub_category($csid,'id,title');
        $categorysubid =  array();
        foreach($sub as $v){
            $categorysubid[$v['id']] = $v['title'];
        }

        $this->display('product/attribute_edit',array(
            'basepath' => '/product/attribute',
            'parent_category' => $parent_class,
            'datas' => $this->mod_attribute_item($id),
            'categorysub' => $categorysubid,
        ));
    }
    private function attribute_add_edit_args(){
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$_POST,'N');
        $args['title'] = $this->existsKeyInArray('title',$_POST,'S');
        $args['defaultvalue'] = $this->existsKeyInArray('defaultvalue',$_POST,'S');
        $args['defaultvaluestyle'] = $this->existsKeyInArray('defaultvaluestyle',$_POST,'N');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$_POST,'N');
        $args['categorysubid'] = $this->existsKeyInArray('categorysubid',$_POST,'N');
        $args['search'] = $this->existsKeyInArray('search',$_POST,'S');
        $args['sort'] = $this->existsKeyInArray('sort',$_POST,'N');
        if(empty($args['title'])) core::error("名称不能为空");
        if(empty($args['categoryid'])) core::error("分类不能为空".$args['categoryid']);
        return $args;
    }
    public function attribute_add_save(){
        if(!$this->post) core::__403();
        $args = $this->attribute_add_edit_args();
        unset($args['id']);
        $insertid = $this->table_row_save('@@product_attribute',$args);
        if(!$insertid) core::error("添加[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/attribute");',
            ),
        ));
    }
    public function attribute_edit_save(){
        if(!$this->post) core::__403();
        $args = $this->attribute_add_edit_args();
        $editid = intval($args['id']);
        unset($args['id']);
        $insertid = $this->table_row_save('@@product_attribute',$args,array('id'=>$editid),true);
        if(!$insertid) core::error("更新[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$editid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/attribute");',
            ),
        ));
    }
    //运费模板设置
    private function freight_template_get_args($get){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    //运费模板分页参数
    private function freight_template_page_args($args){
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    //运费模板列表
    public function freight_template(){
        $get = true;
        $args = $this->freight_template_get_args($get);
        $page_args = $this->freight_template_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_product_freight_template($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);

        $this->display("product/freight_template",array(
            'basepath' => '/product/freight_template',
            'parent_category' => $this->mod_parentcategory(),
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    //新增或编辑参数处理
    private function freight_template_add_edit_args(){
        $args = array();
        $args['id'] = $this->existsKeyInArray('id',$_POST,'N');
        return $args;
    }
    //添加运费模板
    public  function  freight_template_add(){
        $this->display('product/freight_template_add',array(
            'basepath' => '/product/freight_template',
            'maxsort' => $this->table_maxsort('@@freight_template','sort','1'),
        ));
    }
    //编辑运费模板
    public  function  freight_template_edit(){
        $id = intval($_GET['id']);
        $data = $this->mod_freight_template_item($id);
        $this->display('product/freight_template_edit',array(
            'basepath' => '/product/freight_template',
            'data' => $data,
            'parentcategory' => $parentcategory,
        ));
    }
    //添加运费模板-保存
    public  function freight_template_add_save(){
        if(!$this->post) core::__403();
        $para = $this->brands_add_edit_args();
        $insertid = $this->table_row_save('@@freight_template',$para);
        if(!$insertid) core::error("添加失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加成功",
            'auto' => true,
            'yes'  => array(
                'text' => '继续',
            ),
            'no'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/freight_template");',
            ),
        ));
    }
    //编辑运费模板 - 保存
    public function freight_template_edit_save(){
        if(!$this->post) core::__403();
        $para = $this->freight_template_add_edit_args(true);
        $update_args = array('id'=>intval($para['id']));
        unset($para['id']);
        $updateid = $this->table_row_save('@@freight_template',$para,$update_args,true);
        if(!$updateid) core::error("更新[".$para['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$update_args['id'],$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$para['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/freight_template");',
            ),
        ));
    }
    //删除指定的运费模板
    public function freight_template_delete(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        $updateid = $this->mod_freight_template_delete($id);
        if(!$updateid) core::error("删除失败");
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "删除成功",
            'auto' => true,
            'yes'  => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/product/freight_template");',
            ),
        ));
    }
    //////////////////////////////////////////////////////////////////////////////////////
    /// 商品管理
    /// /////////////////////////////////////////////////////////////////////////////////
    //列表-参数采集过滤
    //@param bool $get 提交方式$_GET或$_POST
    private  function  products_get_args($get=true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid',$para,'N');
        $args['categorysubid'] = $this->existsKeyInArray('categorysubid',$para,'N');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        return $args;
    }
    //商品列表搜索-分页参数
    private function products_page_args($args){
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['categoryid'])) $page_args['categoryid'] = $args['categoryid'];
        if(!empty($args['categorysubid'])) $page_args['categorysubid'] = $args['categorysubid'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }
    //商品列表
    public function products(){
        $args = $this->products_get_args(true);
        $page_args = $this->products_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_product_products($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);

        $basepath = '/product/products';
        $parentcategory = $this->mod_parentcategory();
        $subcategory = array();
        if(!empty($args['categoryid']) && !empty($args['categorysubid'])){
            $subcategory = $this->mod_sub_category($args['categoryid'],'id,title');
        }
        $this->display("product/products",array(
            'basepath' => $basepath,
            'parent_category' => $parentcategory,
            'sub_category' => $subcategory,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    //添加商品VIEW
    public  function products_add(){
        $_cid = $this->existsKeyInArray('categoryid',$_REQUEST,'N');
        $_scid = $this->existsKeyInArray('categorysubid',$_REQUEST,'N');
        if(!$_cid) core::error('未指定商品分类，请从商品分类列表中开始添加商品');
        $this->display("product/products_add",array(
            'basepath' => '/product/products',
            'default_cid' => $_cid,
            'default_scid' => $_scid,
            'number' => $this->randNumber(1),
            'uservip' => $this->mod_user_vip(),
            'parent_category' => $this->mod_parentcategory(),
            'sub_category' => $this->mod_sub_category($_cid,'id,title'),
            'attritube_list' => $this->mod_attribute_list($_cid,$_scid),
            'maxsort' => $this->table_maxsort('@@products','sort','1'),
        ));
    }
    //编辑商品VIEW
    public function products_edit(){
        $code = $this->existsKeyInArray('code',$_REQUEST,'S');
        $_cid = $this->existsKeyInArray('categoryid',$_REQUEST,'N');
        $_scid = $this->existsKeyInArray('categorysubid',$_REQUEST,'N');
        if(!$code) core::__403();
        if(!$_cid) core::error('商品分类参数错误，请返回重试');
        $this->display("product/products_edit",array(
            'basepath' => '/product/products',
            'datas' => $this->mod_product_item($code),
            'default_cid' => $_cid,
            'default_scid' => $_scid,
            'uservip' => $this->mod_user_vip(),
            'parent_category' => $this->mod_parentcategory(),
            'sub_category' => $this->mod_sub_category($_cid,'id,title'),
            'attritube_list' => $this->mod_attribute_list($_cid,$_scid),
        ));
    }
    //删除指定商品
    public function products_delete(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        $code = $this->existsKeyInArray('code',$_GET,'S');
        if(!$id) core::__403();
        $delid = $this->mod_products_delete($id,$code);
        if(!$delid) core::error('删除失败');
        $this->addLog($this->authid,__FUNCTION__,$id,$code);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => '$("#btn_refresh").trigger();',
            ),
        ));

    }
    //更新状态
    public function products_state(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        $field = $this->existsKeyInArray('field',$_GET,'S');
        $page_args = array(
            'page' => $this->existsKeyInArray('page',$_GET,'N',1),
            'categoryid' => $this->existsKeyInArray('categoryid',$_GET,'N'),
            'categorysubid' => $this->existsKeyInArray('categorysubid',$_GET,'N'),
        );
        if(!$id || empty($field)) core::__403();
        $updateid = $this->update_state('@@products',$id,$field);
        if(!$updateid) core::error('状态更新失败');
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "状态更新成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/product/products?'.http_build_query($page_args).'");',
            ),
        ));
    }
    //添加或编辑商品时处理参数
    private function products_add_edit_args(){
        $args = array(
            'id' => $this->existsKeyInArray('id',$_POST,'N'),
            'title' => $this->existsKeyInArray('title',$_POST,'S'),
            'code' => $this->existsKeyInArray('code',$_POST,'S'),
            'keys' => $this->existsKeyInArray('keys',$_POST,'S'),
            'icon1' => $this->existsKeyInArray('icon1',$_POST,'S'),
            'icon2' => $this->existsKeyInArray('icon2',$_POST,'S'),
            'icon3' => $this->existsKeyInArray('icon3',$_POST,'S'),
            'icon4' => $this->existsKeyInArray('icon4',$_POST,'S'),
            'icon5' => $this->existsKeyInArray('icon5',$_POST,'S'),
            'pricenet' => $this->existsKeyInArray('pricenet',$_POST,'F'),
            'price' => $this->existsKeyInArray('price',$_POST,'F'),
            'sales' => $this->existsKeyInArray('sales',$_POST,'N'),
            'inventory' => $this->existsKeyInArray('inventory',$_POST,'N'),
            'pricesales' => $this->existsKeyInArray('pricesales',$_POST,'F'),
            'pricescore' => $this->existsKeyInArray('pricescore',$_POST,'F'),
            'salesfromtime' => $this->existsKeyInArray('salesfromtime',$_POST,'N'),
            'salestotime' => $this->existsKeyInArray('salestotime',$_POST,'N'),
            'state' => $this->existsKeyInArray('state',$_POST,'N'),
            'commentstate' => $this->existsKeyInArray('commentstate',$_POST,'N'),
            'sort' => $this->existsKeyInArray('sort',$_POST,'N'),
            'unit' => $this->existsKeyInArray('unit',$_POST,'S'),
            'heft' => $this->existsKeyInArray('heft',$_POST,'F'),
            'reminder' => $this->existsKeyInArray('reminder',$_POST,'S'),
            'categoryid' => $this->existsKeyInArray('categoryid',$_POST,'N'),
            'categorysubid' => $this->existsKeyInArray('categorysubid',$_POST,'N'),
            'recommend' => $this->existsKeyInArray('recommend',$_POST,'N'),
            'buyforvip' => $this->existsKeyInArray('buyforvip',$_POST,'N'),
            'minnum' => $this->existsKeyInArray('minnum',$_POST,'N'),
            'maxnum' => $this->existsKeyInArray('maxnum',$_POST,'N'),
            'website' => $this->existsKeyInArray('website',$_POST,'S'),
            'score' => $this->existsKeyInArray('score',$_POST,'N'),
        );
        if(empty($args['title'])) core::error('商品标题不能为空');
        if(empty($args['keys'])) core::error('商品搜索关键字不能为空');
        if(empty($args['unit'])) core::error('商品的单位不能为空');
        if(empty($args['pricenet'])) core::error('商品的网上售价不能为空');
        if(empty($args['price'])) core::error('商品的当前售价不能为空');
        //如果没有图示的将默认不展示
        if(empty($args['icon1']) || $args['icon1']=='#') $args['state'] = 0;
        $args_con = array(
            'content' => $this->existsKeyInArray('content',$_POST,'S'),
            'code' => $args['code'],
        );
        return array(
            'args' => $args,
            'cont' => $args_con,
        );
    }
    //添加商品保存事件
    public function products_add_save(){
        if(!$this->post) core::__403();
        $args = $this->products_add_edit_args();
        //添加商品信息到主表
        unset($args['args']['id']);
        $insertid = $this->table_row_save('@@products',$args['args']);
        if(!$insertid) core::error("添加[ {$args['args']['title']} ]失败");
        //添加商品详情到详情表
        $this->table_row_save_replace('@@products_con', $args['cont']);
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[ {$args['args']['title']} ]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '继续添加',
                'func' => 'JumpMainFrame("/product/products_add?categoryid='.$args['args']['categoryid'].'&categorysubid='.$args['args']['categorysubid'].'");',
            ),
            'no'  => array(
                'text' => '查看商品',
                'func' => 'JumpMainFrame("/product/products?categoryid='.$args['args']['categoryid'].'&categorysubid='.$args['args']['categorysubid'].'");',
            ),
        ));
    }
    public function products_edit_save(){
        if(!$this->post) core::__403();
        $args = $this->products_add_edit_args();
        //var_dump($args['args']);
        //exit;
        $id = intval($args['args']['id']);
        $code = $args['args']['code'];
        unset($args['args']['id']);
        unset($args['args']['code']);
        //添加商品信息到主表
        $updateid = $this->table_row_save('@@products',$args['args'],array('id'=>$id),true);
        //添加商品详情到详情表
        $this->table_row_save_replace('@@products_con', $args['cont']);
        $this->addLog($this->authid,__FUNCTION__,$id,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[ {$args['args']['title']} ]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/product/products?categoryid='.$args['args']['categoryid'].'&categorysubid='.$args['args']['categorysubid'].'");',
            ),
        ));
    }
    /////////////////////////////////////////////////////////////////////////////////////
}